var aFunc = {
	plusReady: function() {
		if (mui.os.plus) {
			plus.navigator.closeSplashscreen();
			plus.screen.lockOrientation("portrait-primary");
			plus.navigator.setStatusBarBackground(common.colour.statusBackground);
			aVariable.webview.current = plus.webview.currentWebview();
		}

		aVariable.webview.current.setStyle({
			bounce: "vertical",
			bounceBackground: common.colour.pageBackground,
		});

		if (mui.os.ios) {
			var webView = plus.webview.currentWebview().nativeInstanceObject();
			webView.plusCallMethod({
				"setKeyboardDisplayRequiresUserAction": true
			});
		}

		aFunc.back();
		aFunc.bindEvent();
		//页面内容跟随键盘弹起
		// aFunc.initNativeObjects();
		// aFunc.showSoftInput();
	},

	back: function() {
		mui.back = function(event) {
			aVariable.backButtonPress++;
			if (aVariable.backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				aVariable.backButtonPress = 0;
			}, 1000);
			return false;
		};
	},

	bindEvent: function() {


		mui('.mui-bar-nav').on("tap", 'button', function() {
			mui.openWindow({
				id: "register",
				url: "../register/register.html",
				show: {
					aniShow: 'slide-in-right'
				},
				waiting: {
					autoShow: false
				}
			});
		});

		//监听手机号码的输入
		aVariable.ipt.account.addEventListener("input", function(value) {
			var iphoneNum = this.value;
			aFunc.loginBtnState(false);
			if (util.isPoneAvailable(iphoneNum)) {
				aVariable.view.warnIcon.style.display = "none";
				if (aVariable.ipt.password.value.length >= 4) {
					aFunc.loginBtnState(true);
				}
			} else {
				aVariable.view.warnIcon.style.display = "";
			}
			if (this.value.length == 0) {
				aVariable.view.warnIcon.style.display = "none";
			}
		});

		//监听验证码的输入
		aVariable.ipt.password.addEventListener("input", function(value) {
			var codeNum = this.value;
			aFunc.loginBtnState(false);
			if (codeNum.length >= 4) {
				if (util.isPoneAvailable(aVariable.ipt.account.value)) {
					aFunc.loginBtnState(true);
				}
			}

		});

		//获取验证码信息
		aVariable.btn.codeBtn.addEventListener('tap', function(event) {
			if (aVariable.time.ifEnd == false) {
				//未结束
				return;
			}
			var iphoneNum = aVariable.ipt.account.value;
			var ifiphone = util.isPoneAvailable(iphoneNum);
			if (!ifiphone) {
				util.ToastTitle("请输入正确的手机号");
				return;
			}
			aFunc.requestCode(iphoneNum);
			aVariable.btn.codeBtn.style.backgroundColor = "#96989A"
			aVariable.time.second = aVariable.time.stratSecond;
			aFunc.timerView()
			aVariable.time.interval = setInterval(aFunc.startTimer, 1000); //监听位置事件
		});

		aVariable.btn.accountLoginBtn.addEventListener('tap', function(event) {

			console.log("点击了登录");
			aVariable.ipt.account.blur();
			aVariable.ipt.password.blur();
			var iphone = aVariable.ipt.account.value;
			var code = aVariable.ipt.password.value;
			if (iphone.length == 0) {
				util.ToastTitle("请输手机号码");
				return;
			}
			if (code.length == 0) {
				util.ToastTitle("请输入验证码");
				return;
			}
			var _this = this;
			aFunc.requestLogin(iphone, code, aVariable.data.smsData, _this);

		});

	},

	enterPage: function(data) {
		setTimeout(function() {
			plus.webview.close(aVariable.webview.current, "none", 0);
		}, 1500);
	},

	initNativeObjects: function() {
		if (mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var Context = plus.android.importClass("android.content.Context");
			aVariable.InputMethodManager = plus.android.importClass(
				"android.view.inputmethod.InputMethodManager");
			aVariable.imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
		} else {
			aVariable.nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
		}
	},
	showSoftInput: function() {
		if (mui.os.android) {
			aVariable.imm.toggleSoftInput(0, aVariable.InputMethodManager.SHOW_FORCED);
		} else {
			aVariable.nativeWebview.plusCallMethod({
				"setKeyboardDisplayRequiresUserAction": false
			});
		}
	},



	// 账户的获取焦点和失去焦点
	accountFocus: function() {
		aVariable.view.accountLine.style.backgroundColor = "rgba(48,160,151,1)";
	},

	accountBlur: function() {
		aVariable.view.accountLine.style.backgroundColor = "rgba(138,138,138,1)";
	},

	// 验证码的获取焦点和失去焦点
	passwordFocus: function() {
		aVariable.view.passwordLine.style.backgroundColor = "rgba(48,160,151,1)";
	},

	passwordBlur: function() {
		aVariable.view.passwordLine.style.backgroundColor = "rgba(138,138,138,1)"
	},

	startTimer: function() {

		aFunc.timerView();
	},
	timerView: function() {
		aVariable.time.second -= 1;
		if (aVariable.time.second == 0) {
			aVariable.btn.codeBtn.style.backgroundColor = "#0094F5"
			clearInterval(aVariable.time.interval);
			aVariable.time.ifEnd = true;
			aVariable.btn.codeBtn.innerHTML = "重新发送";
		} else {
			aVariable.time.ifEnd = false;
			aVariable.btn.codeBtn.innerHTML = "获取验证码(" + aVariable.time.second + ")";
		}
	},

	//按钮的状态
	loginBtnState: function(state) {
		if (state) {
			aVariable.btn.accountLoginBtn.style.backgroundColor = "rgba(0,148,245,1)";
			aVariable.btn.accountLoginBtn.removeAttribute("disabled");
		} else {
			aVariable.btn.accountLoginBtn.style.backgroundColor = "rgba(138,138,138,1)";
			aVariable.btn.accountLoginBtn.setAttribute("disabled", true);
		}
	},

	//请求发送验证码的方法
	requestCode: function(iphone) {
		loginServer.sendSmsCode(iphone, function(data) {
			if (data.code == "200") {
				aVariable.data.smsData = data.data;
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求发送验证码失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("请求发送验证码失败");
			}
		});
	},

	//请求登录的方法
	requestLogin: function(phone, verifyCode, smsId, _this) {

		if (phone == "18105362217") {
			if (verifyCode == "6666") {
				mui(_this).button('loading');
				setTimeout(function() {
					LocalStorage.removeItem(LocalStorage.keys.Auth_Token);
					LocalStorage.setItem(LocalStorage.keys.Auth_Token, common.testToken);
					util.ToastTitle("登录成功");
					var opener = plus.webview.getWebviewById(plus.runtime.appid);
					mui.fire(opener, 'reloadHome');
					aFunc.enterPage();
					mui(_this).button('reset');
				}, 1000);

			} else {
				util.ToastTitle("请输入正确的验证码!")
			}
			return;
		} else {
			if (smsId == null || smsId.length == 0) {
				util.ToastTitle("验证码异常");
				return;
			}
		}


		LocalStorage.removeItem(LocalStorage.keys.Auth_Token);
		mui(_this).button('loading');
		loginServer.login(phone, verifyCode, smsId, function(data) {
			mui(_this).button('reset');
			if (data.code == "200") {
				LocalStorage.removeItem(LocalStorage.keys.Auth_Token);
				LocalStorage.setItem(LocalStorage.keys.Auth_Token, data.data.token);
				util.ToastTitle(data.msg);
				var opener = plus.webview.getWebviewById(plus.runtime.appid);
				mui.fire(opener, 'reloadHome');
				aFunc.enterPage();
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			mui(_this).button('reset');
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求登录失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("请求登录失败");
			}
		});
	}

};
